<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="query">
				<el-form-item>
					<el-input v-model="query.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button icon="el-icon-search" type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-popconfirm icon="el-icon-delete" title="确定删除吗？" @confirm="reallyChecked" style="margin-left: 5px;">
						<el-button slot="reference" type="danger">批量删除</el-button>
					</el-popconfirm>
					<!-- <el-button type="danger" v-on:click="batchDelete">批量删除</el-button> -->
				</el-form-item>

			</el-form>

		</el-col>

		<!--列表-->
		<template>
			<el-table :data="pageInfo.shops" highlight-current-row v-loading="loading" style="width: 100%;"
				@selection-change="handleSelectionChange">
				<el-table-column type="selection" width="50">
				</el-table-column>
				<el-table-column prop="name" label="店铺名" width="130" sortable>
				</el-table-column>
				<el-table-column prop="tel" label="电话" width="130" sortable>
				</el-table-column>
				<el-table-column prop="registerTime" label="注册时间" width="130" sortable>
				</el-table-column>
				<el-table-column prop="state" label="状态" width="130" sortable>
          <template slot-scope="scope">
            <el-tag
                :type="scope.row.state === 1 ? 'info' : (scope.row.state === 2 ? 'primary' : (scope.row.state === 3 ? 'success' : 'danger'))"
                disable-transitions>{{ scope.row.state === 1 ? '待审核' :
                (scope.row.state === 2 ? '待激活' : (scope.row.state === 3 ? '激活成功' : '驳回')) }}</el-tag>
          </template>
				</el-table-column>
        <el-table-column prop="address" label="地址" width="130" sortable>
        </el-table-column>
        <el-table-column prop="logo" label="LOGO" width="130" sortable>
          <template slot-scope="scope">
            <el-image
                style="width: 70px; height: 70px"
                :src="scope.row.logo"
                :preview-src-list="srcList">
            </el-image>
          </template>
        </el-table-column>
				<el-table-column label="操作" width="250">
					<template scope="scope">
						<el-button type="success" size="small" @click="handleEdit(scope.row)">编辑</el-button>
						<el-popconfirm icon="el-icon-delete" title="确定删除吗？" @confirm="handleDel(scope.row.id)"
							style="margin-left: 5px;">
							<el-button slot="reference" size="small" type="danger">删除</el-button>
						</el-popconfirm>
					</template>
				</el-table-column>
			</el-table>
		</template>
		<el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
			:current-page="this.query.pageNumber" :page-sizes="[5, 10, 20, 40]" :page-size=this.query.pageSize
			layout="total, sizes, prev, pager, next, jumper" :total=this.pageInfo.total>
		</el-pagination>

		<el-dialog title="角色信息" :visible.sync="dialogFormVisible" :close-on-click-modal="false"
			:close-on-press-escape="false" @close="cancel">
      <el-form :model="shop" ref="shopForm" :rules="formRules" label-position="left" label-width="100px" class="demo-ruleForm login-container">
        <h3 class="title">店铺入驻</h3>
        <el-form-item prop="name"label="名称">
          <el-input type="text" v-model="shop.name" auto-complete="off" placeholder="请输入店铺名称！"></el-input>
        </el-form-item>
        <el-form-item prop="tel" label="座机">
          <el-input type="text" v-model="shop.tel" auto-complete="off" placeholder="请输入座机！"></el-input>
        </el-form-item>
        <el-form-item prop="address" label="店铺地址">
          <el-input type="text" v-model="shop.address" auto-complete="off" placeholder="请输入地址！"></el-input>
<!--          <el-button size="small" type="primary" @click="selectAddress">选择</el-button>-->
        </el-form-item>
        <el-form-item prop="logo" label="店铺Logo">
          <el-input type="text" v-model="shop.logo" auto-complete="off" placeholder="请输入logo！"></el-input>
          <!--
          action 它是后端文件上传接口的地址, 默认的请求方式是post
          on-preview="handlePreview"  预览图片时,触发handlePreview
          on-remove="handleRemove"  删除图片时,触发handleRemove
          on-success="handleSuccess" 上传成功时,触发handleSuccess
          -->
          <!--        <el-upload-->
          <!--            class="upload-demo"-->
          <!--            action="http://localhost:8081/fastDfs/upload"-->
          <!--            :on-preview="handlePreview"-->
          <!--            :on-remove="handleRemove"-->
          <!--            :on-success="handleSuccess"-->
          <!--            :file-list="fileList"-->
          <!--            list-type="picture">-->
          <!--          <el-button size="small" type="primary">点击上传</el-button>-->
          <!--          <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>-->
          <!--        </el-upload>-->
        </el-form-item>
        <h3 class="title">管理员信息设置</h3>
<!--        <el-form-item prop="admin.username" label="账号">-->
<!--          <el-input type="text" v-model="shop.admin.username" auto-complete="off" placeholder="请输入账号！"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item prop="admin.phone" label="手机号码">-->
<!--          <el-input type="text" v-model="shop.admin.phone" auto-complete="off" placeholder="请输入电话！"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item prop="admin.email" label="电子邮件">-->
<!--          <el-input type="text" v-model="shop.admin.email" auto-complete="off" placeholder="请输入邮件！"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item prop="admin.password" label="密码">-->
<!--          <el-input type="password" v-model="shop.admin.password" auto-complete="off" placeholder="请输入密码！"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item prop="admin.confirmPassword" label="确认密码">-->
<!--          <el-input type="password" v-model="shop.admin.confirmPassword" auto-complete="off" placeholder="请输入确认密码！"></el-input>-->
<!--        </el-form-item>-->
        <el-form-item style="width:100%;">
          <el-button type="primary" style="width:100%;" @click.native.prevent="settledIn" >入驻</el-button>
        </el-form-item>
      </el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="cancel">取 消</el-button>
				<el-button type="primary" @click="save">确 定</el-button>
			</div>
		</el-dialog>

	</section>
</template>
<script>
//import NProgress from 'nprogress'
export default {
	data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.shop.admin.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
		return {
      keyword:'',
      dialogFormVisible: false,
			loading: false,
			addLoading: false,
			query: {
				pageNumber: 1,
				pageSize: 5,
				keyword: ''
			},
			pageInfo: {
				total: 0,
				shops: []
			},
			selectColumn: [],
			form: {
				id: null,
				title: '',
				value: '',
				sequence:'',
				intro:'',
				parent:{
					id:null,
					title:''
				}
			},
			formLabelWidth: '120px',
      srcList: [
        'http://115.159.217.249:8888/group1/M00/00/07/rBEAA2Mi5nmASpVNAAHt5Ab1hek042.jpg',
        'http://localhost:8080/static/images/head/avatar.png',
        'http://115.159.217.249:8888/group1/M00/00/08/rBEAA2NigfGAWCvGAAQWyeWUl_c61.jpeg',
        'http://115.159.217.249:8888/group1/M00/00/0F/rBEAA2PWFCyAJHDJAAHr0nJ1jWY867.jpg'
      ],
      shop: {
        admin:{
          username:"",
          phone:"",
          email:"",
          password:"",
          confirmPassword:""
        },
        name: '',
        tel: '',
        address: '',
        logo:''
      },
      formRules: {
        name: [
          { required: true, message: '请输入店铺名称!', trigger: 'blur' }
        ],
        tel: [
          { required: true, message: '请输入店铺座机!', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请输入店铺地址!', trigger: 'blur' }
        ],
        'admin.phone': [
          { required: true, message: '请输入手机号!', trigger: 'blur' }
        ],
        // logo: [
        //     { required: true, message: '请输入店铺logo!', trigger: 'blur' }
        // ],
        'admin.email': [
          { type: 'email',required: true, message: '请输入邮箱!', trigger: 'blur' }
        ],
        'admin.username': [
          { required: true, message: '请输入账号!', trigger: 'blur' }
        ],
        'admin.password': [
          { required: true, message: '请输入密码!', trigger: 'blur' }
        ],
        'admin.confirmPassword': [
          {required: true,validator: validatePass2, trigger: 'blur' } //自定义校验规则
        ]
      }

		}
	},
	methods: {
    settledIn(){
      this.$refs.shopForm.validate((valid) => {
        //校验表单成功后才做一下操作
        if (valid) {
          this.$confirm('确认入驻吗？', '提示', {}).then(() => {
            //拷贝后面对象的值到新对象,防止后面代码改动引起模型变化
            let para = Object.assign({}, this.shop); //shop 本身这个参数里面就有店铺和管理员信息
            // 为了后台好接收，封装一个对象放到里面
            //判断是否有id有就是修改,否则就是添加
            this.$http.post("/shop/settlement",para)
                .then((res) => res.data).then(res=>{
              console.log(res)
              if(res.code == 200){
                this.$message({
                  message: '操作成功!',
                  type: 'success'
                });
                //重置表单
                this.$refs['shopForm'].resetFields();
                //跳转登录页面
                this.$router.push({ path: '/login' });
              }
              else{
                this.$message({
                  message: res.message,
                  type: 'error'
                });
              }

            });
          });
        }
      })
    },
		//编辑取消方法
		cancel() {
			this.dialogFormVisible = false
			this.getShops()
		},
		//编辑按钮触发事件
		handleEdit(row) {
			let temp = Object.assign({}, row);
			this.shop = temp
			this.dialogFormVisible = true
		},

		//保存信息网络请求
		save() {
			let addForm = this.form
			this.$http.post('/shop', addForm)
				.then(res => {
					//console.log(res);
					if (res.data.code == 200) {
						this.dialogFormVisible = false
						this.$message({ message: res.data.message, type: 'success' });
						this.getShops();
					} else {
						this.$message({ message: res.data.message, type: 'danger' });
					}
				}).catch(res => {
					this.$message({ message: '网络异常', type: 'error' });
				})
		},
		reallyChecked() {
			var ids = this.selectColumn.map(x => x.id);
			if (ids.length == 0) {
				this.$message({ message: '未选中消息', type: 'warning' });
				//return ;
			} else {
				this.batchDelete(ids)
			}
		},
		//批量删除
		batchDelete(ids) {
			//var ids = this.selectColumn.map(x => x.id);
			this.$http.patch("/shop", ids)
				.then(res => {
					if (res.data.code == 200) {
						this.$message({ message: '删除成功', type: 'success' });
						this.query.pageNumber = 1
						this.getShops();
					} else {
						this.$message({ message: '请联系管理员!', type: 'warning' });
					}
				}).catch(res => {
					this.$message({ message: '糟糕糟糕偶买噶，魔法怎么失灵了', type: 'warning' });
				})

		},

		handleSelectionChange(val) {
			this.selectColumn = val
		},
		handleDel(id) {
			this.$http.delete("/shop/" + id)
				.then(res => {
					if (res.data.code == 200) {
						this.$message({ message: '删除成功', type: 'success' });
						this.query.pageNumber = 1
						this.getShops();
					} else {
						this.$message({ message: '请联系管理员!', type: 'danger' });
					}
				}).catch(res => {
					this.$message({ message: '糟糕糟糕偶买噶，魔法怎么失灵了', type: 'warning' });
				})

		},

		search() {
			this.query.pageNumber = 1;
			this.getShops();
		},
		handleSizeChange(val) {
			this.query.pageSize = val;
			this.query.pageNumber = 1;
			this.getShops();
		},
		handleCurrentChange(val) {
			this.query.pageNumber = val;
			this.getShops();
		},
		//获取部门列表
		getShops() {
			this.$http.put('/shop', this.query)
				.then(res => res.data).then(res => {
					if (res.code == 200) {
						this.pageInfo.shops = res.data.pageList.rows
						this.pageInfo.total = res.data.pageList.total
					}
				})
				.catch(res => {
					console.log(res);
				})
		}
	},
	mounted() {
		this.getShops();
	}
};

</script>

<style scoped></style>